const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database/maix.sqlite3');

function getUser(uid) {
    let uidNum = parseInt(uid);
    return new Promise((resolve, reject) => {
        db.all(`SELECT * FROM User where id = ${uidNum}`, (err, row) => {
            if (err || row.length === 0) {
                //如果是用async/await，调用resolve
                resolve(null);
                //如果是用Promise，调用reject
                //reject(err);
            } else {
                resolve(row[0]);
            }
        })
    })
}

function setLesson(data) {
    let cmd = 
    [
        `UPDATE KV SET value='${data.lesson}' WHERE KV.key='lesson';`,
        `UPDATE KV SET value='${data.project}' WHERE KV.key='project';`,
        `UPDATE KV SET value='${data.mission}' WHERE KV.key='mission';`,
        `UPDATE KV SET value='${data.sensorThes.temp}' WHERE KV.key='ref_temp';`,
        `UPDATE KV SET value='${data.sensorThes.humi}' WHERE KV.key='ref_humi';`,
        `UPDATE KV SET value='${data.sensorThes.pm25}' WHERE KV.key='ref_pm25';`,
        `UPDATE KV SET value='${data.sensorThes.voc}' WHERE KV.key='ref_voc';`,
        `UPDATE KV SET value='${data.sensorThes.pressure}' WHERE KV.key='ref_pressure';`,
        `UPDATE KV SET value='${data.sensorThes.oxygen}' WHERE KV.key='ref_oxygen';`
    ]
    console.log(cmd);
    return new Promise((resolve, reject) => {
        let cb = (err) => {
            if (err) {
                //如果是用async/await，调用resolve
                reject(err);
                //如果是用Promise，调用reject
                //reject(err);
            } else {
                resolve();
            }
        }
        for(i in cmd){
            db.run(cmd[i], cb)
        }
    })
}
function getLesson() {
    return new Promise((resolve, reject) => {
        let cmd = 
        `SELECT value FROM KV WHERE KV.key='lesson'` + 
        `OR KV.key='project'` +
        `OR KV.key='mission'` +
        `OR KV.key='ref_temp'` +
        `OR KV.key='ref_humi'` +
        `OR KV.key='ref_pm25'` +
        `OR KV.key='ref_voc'` +
        `OR KV.key='ref_pressure'` +
        `OR KV.key='ref_oxygen'`;
        db.all(cmd, (err, row) => {
            if (err || row.length === 0) {
                //如果是用async/await，调用resolve
                resolve(null);
                //如果是用Promise，调用reject
                //reject(err);
            } else {
                let res = {
                    lesson: row[0].value,
                    project: row[1].value,
                    mission: row[2].value,
                    sensorThes: { 
                        temp: row[3].value,
                        humi: row[4].value,
                        pm25: row[5].value,
                        voc: row[6].value,
                        pressure: row[7].value,
                        oxygen: row[8].value 
                    }
                }
                resolve(res);
            }
        })
    })
}

module.exports = {
    getUser,
    setLesson,
    getLesson
};

// (async ()=>{
//     let userInfo = await getUser(20210707)
//     console.log(userInfo)
//     userInfo = await getUser(202107037)
//     console.log(userInfo)
// })()